﻿using System;
using System.Collections.Generic;
using Abp.Application.Services;
using Jiguang.JPush;
using Jiguang.JPush.Model;
using Abp.Dependency;
using FwProject.JPush.JPushModel;
using Castle.Core.Logging;

namespace FwProject.PublicService.JPush
{

    public class JPushAppservice :IJPushAppservice,ISingletonDependency
    {
        private readonly  JPushClient client;
        public ILogger logger { get; set; }
        public JPushAppservice(JPushConfig jPushConfig)
        {
            logger = NullLogger.Instance;
            try
            {
                if (jPushConfig == null)
                {
                    logger.Info($"{nameof(jPushConfig)}空对象");
                    jPushConfig = IocManager.Instance.Resolve<JPushConfig>();
                }

                client = new JPushClient(jPushConfig.AppKey, jPushConfig.AppSecret);
            }
            catch (Exception exe)
            {

                logger.Info(exe.Message);
            }
           
        }
        /// <summary>
        /// 餐厅通知
        /// </summary>
        public void RestaurantPushMessage(string mobile, IBasePushMessage dic, string Alert= "饭碗易购消息提示", string title= "饭碗易购消息提示",string content= "饭碗易购消息提示")
        {
            NewMethod(mobile, dic.GetDictionary(), Alert, title, content);

        }
        /// <summary>
        /// 消息推送
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="dic"></param>
        /// <param name="Alert"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        private void NewMethod(string mobile, Dictionary<string, string> dic, string Alert, string title, string content)
        {
            Audience audience = new Audience();
            audience.Alias = new List<string>();
            audience.Alias.Add(mobile);
            PushPayload pushPayload = new PushPayload()
            {
                Platform = new List<string> { "android", "ios" },
                Audience = audience,

                Notification = new Notification
                {
                    Alert = "饭碗易购消息提示",
                    Android = new Android
                    {
                        Alert = Alert,
                        Title = title,
                        Style = 3,
                        BigPicturePath = "https://service.fanwanyigou.com/swagger/ui/logo.png"
                    },
                    IOS = new IOS
                    {
                        Alert = Alert,
                        Badge = "+1"
                    },

                },
                Message = new Message
                {
                    Title = title,
                    Content = content,
                    Extras = dic
                },

                Options = new Options
                {
#if DEBUG
                    IsApnsProduction = false
#else
                     IsApnsProduction = true // 设置 iOS 推送生产环境。不设置默认为开发环境。
#endif

                }
            };
            try
            {
                var response = client.SendPush(pushPayload);

                logger.Info($"推送接受者：{mobile}==={response.Content}==={response.Headers}===={response.StatusCode}");
            }
            catch (Exception exe)
            {

                logger.Info(exe.Message);
            }
        }
        /// <summary>
        /// 商城消息推送
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="dic"></param>
        /// <param name="Alert"></param>
        /// <param name="title"></param>
        /// <param name="content"></param>
        public void ShopApp(string mobile, IBasePushMessage dic, string Alert = "饭碗易购商城消息提示", string title = "饭碗易购商城消息提示", string content = "饭碗易购商城消息提示") {

            NewMethod(mobile, dic.GetDictionary(), Alert, title, content);
        }

        /// <summary>
        /// 司机端通知
        /// </summary>
        public void DriverPushMessage(){


        }
       /// <summary>
       /// 库管端通知
       /// </summary>
        public void StorehousestaffPushMessage(){


        }
    }
}
